package o2o.web.actions.system;

import com.jinyou.utils.common.AjaxResult;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.init.AutoWiredClass;
import o2o.dao.PO.user.UserInfo;
import o2o.task.order.OrderBakTaskUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class System {
    @Autowired
    private AutoWiredClass autoWiredClass;


    @RequestMapping(method = RequestMethod.POST, value = "/sys/memory/reload")
    @ResponseBody
    public String advert_list(String code) {
//        InitLoadDataServlet.initLoad();

        autoWiredClass.initCache();
        return AjaxResult.successResult();
    }


    // 加载sql
    @RequestMapping(method = RequestMethod.POST, value = "/sys/sql/load")
    @ResponseBody
    public String sql_load(String code) {
//        SysInitAutoSQLUtils.initAutoSql();
        return AjaxResult.successResult();
    }

    // 删除已备份订单
    @RequestMapping(method = RequestMethod.POST, value = "/sys/order/del")
    @ResponseBody
    public String del_order(String token, Integer days) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!"sadmin".equalsIgnoreCase(user.getUsername())) {
            return AjaxResult.errorResult("您没有权限");
        }

        OrderBakTaskUtils.delOldOrder(days);

        return AjaxResult.successResult();
    }

    // 备份订单
    @RequestMapping(method = RequestMethod.POST, value = "/sys/order/bak")
    @ResponseBody
    public String bak_order(String token, Integer days) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        OrderBakTaskUtils.autoBackOrder(days);

        return AjaxResult.successResult();
    }


}
